//
//  GesturePasswordControllerViewController.swift
//  GesturePassword4Swift
//
//  Created by feiin on 14/11/22.
//  Copyright (c) 2014年 swiftmi. All rights reserved.
// 
//  Edited by Glitter on 16/01/12.

import UIKit


class GesturePasswordViewController: UIViewController, TentacleViewDelegate {
     
    var gesturePasswordView: GesturePasswordView!
    var previousString: String? = ""
    var password_UDefault: String? = ""
    var userKey4gesture: String = USERINFO.usrid.unwrap
   
    var goNextVC:((_ flag: Bool) -> Void)?
    var backBut: UIButton!
    var faceIDBtn : UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
      
        
        previousString = ""
        // 键 用户id   值  手势密码的数字替换
        password_UDefault = UserDefaults.standard.object(forKey: userKey4gesture) as? String
        
        gesturePasswordView = GesturePasswordView(frame: UIScreen.main.bounds)
        gesturePasswordView.forgetClosure = {
            go2SmsVeri(sms_type: "2") {
                self.gesturePasswordView.state?.textColor = UIColor.white
                self.gesturePasswordView.state?.text = "修改手势密码"
                
                self.resetGeseture()
            }
            
            
        }
        view.addSubview(gesturePasswordView)
        
        if password_UDefault.unwrap.isEmpty {
            gesturePasswordView.state?.textColor = UIColor.white
           gesturePasswordView.state?.text = "设置手势密码"
           resetGeseture()
        } else {
            verify()
            
         }
        
        backBut = UIButton()
        backBut.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        backBut.setTitleColor(UIColor.white, for: UIControl.State())
        backBut.setTitle("返回", for: UIControl.State())
        backBut.addTarget(self, action: #selector(backVC), for: .touchDown)
        view.addSubview(backBut)
        backBut.snp.makeConstraints { make in
            make.left.equalTo(view).offset(15)
            make.top.equalTo(view).offset(46)
            make.width.equalTo(50)
            make.height.equalTo(30)
        }
        
//        if BioMetricAuthenticator.shared.faceIDAvailable() {
//            faceIDBtn = UIButton()
//            faceIDBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//            faceIDBtn.setTitleColor(UIColor.white, for: UIControl.State())
//            faceIDBtn.setTitle("faceID校验", for: UIControl.State())
//            faceIDBtn.addTarget(self, action: #selector(faceIDAction), for: .touchDown)
//            view.addSubview(faceIDBtn)
//            faceIDBtn.snp.makeConstraints { make in
//                make.right.equalTo(view).offset(-15)
//                make.top.equalTo(view).offset(46)
//                make.width.equalTo(80)
//                make.height.equalTo(30)
//            }
//        }
        
        
    }
    

    
    @objc fileprivate func backVC(_ flag:Bool) {
        
        dismiss(animated: true, completion: { [unowned self] in
            self.goNextVC!(flag)
        })
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - 验证手势密码
    func verify() {
        gesturePasswordView.state?.textColor = UIColor.white
        gesturePasswordView.state?.text = "验证手势密码"
        //        gesturePasswordView = GesturePasswordView(frame: UIScreen.mainScreen().bounds)
        gesturePasswordView.tentacleView!.delegate = self
        gesturePasswordView.tentacleView!.styleX = .verify
 
    }
    
    // MARK: - 重置手势密码 或新设置手势
    fileprivate func resetGeseture() {
        
        gesturePasswordView.tentacleView!.delegate = self
        gesturePasswordView.tentacleView!.styleX = .set
        gesturePasswordView.forgetButton!.isHidden = true
     }
    
    func exist() -> Bool {
        password_UDefault = UserDefaults.standard.object(forKey: userKey4gesture) as? String
        if password_UDefault == USERINFO.usrid.unwrap {
            return true
        }
        return false
        
    }
    
    // MARK: - 清空记录
    func clear() {
        
        UserDefaults.standard.removeObject(forKey: userKey4gesture)
    }
    
   
    func gestureSetFail() {
        gesturePasswordView.state?.textColor = UIColor.red
        gesturePasswordView.state?.text = "手势密码设置错误"
     }
    func gestureTouchBegin() {
  
    }
    func verification(_ result: String) -> Bool {
        
        if result == password_UDefault {
            
            gesturePasswordView.state?.textColor = UIColor.white
            gesturePasswordView.state?.text = "输入正确"
            backVC(true)
            
            return true
        }
        gesturePasswordView.state?.textColor = UIColor.red
        gesturePasswordView.state?.text = "手势密码错误"
        gesturePasswordView.tentacleView!.touchedArray.removeAll()
        
        return false
    }
    
    func resetPassword(_ result: String,times:Int) -> Bool {
        
        if times == 1 {
            gesturePasswordView.state?.textColor =  UIColor.white
            gesturePasswordView.state?.text = "再次输入手势密码"
            UserDefaults.standard.set(result, forKey: userKey4gesture)
            return true
        } else {
            if result ==  UserDefaults.standard.object(forKey: userKey4gesture) as? String {
                gesturePasswordView.state?.textColor = UIColor(red: 2/255, green: 174/255, blue: 240/255, alpha: 1)
               gesturePasswordView.state?.text = "已保存手势密码"
                backVC(true)
                return true
            } else {
                self.clear()
                gesturePasswordView.tentacleView?.times = 1
                gesturePasswordView.state?.textColor = UIColor(red: 2/255, green: 174/255, blue: 240/255, alpha: 1)
               gesturePasswordView.state?.text = "两次密码不同，请重新设置"
                 return false
            }
            
        }
 
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return UIInterfaceOrientation.portrait
    }
    
}

